/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package assignment2.data;

import java.io.Serializable;
import java.util.Collection;
import java.util.Iterator;

/**
 *
 * @author Thanh
 */
public class Order implements Serializable{
    String productId;
    int quantity;

    public Order(String productId, int quantity) {
        this.productId = productId;
        this.quantity = quantity;
    }

    public Order() {
    }

    public String getProductId() {
        return productId;
    }

    public void setProductId(String productId) {
        this.productId = productId;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public Order(String productId) {
        this.productId = productId;
    }
    public static boolean checkExistOrder(String productId, Collection<Order> list) {
        if (!list.isEmpty()) {
            Iterator<Order> iter = list.iterator();
            while (iter.hasNext()) {
                Order entity = iter.next();
                if (entity.getProductId().equals(productId)) {
                    Order newen = new Order(entity.getProductId(), entity.getQuantity() + 1);
                    list.remove(entity);
                    list.add(newen);
                    return true;
                }
            }
        }
        return false;
    }

    public static Order searchOrder(String productId, Collection<Order> list) {
        Order order = null;
        if (!list.isEmpty()) {
            Iterator<Order> iter = list.iterator();
            while (iter.hasNext()) {
                Order entity = iter.next();
                if (entity.getProductId().equals(productId)) {
                    order = entity;
                }
            }
        }
        return order;
    }
}
